package com.teamwork.shareplatform.mapper;

import com.teamwork.shareplatform.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
//@Repository
public interface UserMapper {
    // 查询所有用户
    @Select("SELECT * FROM users")
    List<User> findAll();

    // 根据登录信息查找用户
    @Select("SELECT * FROM users WHERE telphone = #{telphone} AND password = #{password} AND utype = #{utype}")
    User findBylogin(String telphone, String password, int utype);

    // 添加用户
    @Insert("INSERT INTO users(telphone, password, utype, username) VALUES(#{telphone}, #{password}, 0, #{username})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    int addUser(User user);

    // 根据 ID 查询用户
    @Select("SELECT * FROM users WHERE id = #{id}")
    User queryById(int id);

    // 查询所有普通用户（utype = 0）
    @Select("SELECT * FROM users WHERE utype = 0 LIMIT #{offset}, #{limit}")
    List<User> queryAllUser(@Param("offset") int offset, @Param("limit") int limit);

    // 通过编辑信息添加用户
    @Insert("INSERT INTO users(telphone, password, utype, username, tschool, sex, qq, email) VALUES(#{telphone}, #{password}, 0, #{username}, #{tschool}, #{sex}, #{qq}, #{email})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    int addUserByEdit(User user);

    // 更新用户信息
    @Update("UPDATE users SET username = #{username}, tschool = #{tschool}, email = #{email}, sex = #{sex}, qq = #{qq}, password = #{password} WHERE id = #{id}")
    int updateUser(User user);

    // 根据 ID 删除用户
    @Delete("DELETE FROM users WHERE id = #{id}")
    int deleteUser(int id);

    List<User> queryAllUser();
}